﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Keepo.Models;
using Keepo.DAL;
using System.Data.Entity;
using Infrastructure.Data;
using Keepo.Helpers;
using AlbumDesigner.Helpers;
using System.Text;

namespace AlbumDesigner
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //filters.Add(new HandleErrorAttribute());

            //add attribute
            filters.Add(new LoadRememberMeAttribute());
            filters.Add(new CookieGuidAttribute());
            filters.Add(new SaveLastLoginAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            // routes.IgnoreRoute("{*weborb}", new { weborb = @".*weborb\.aspx(/.*)?" });
            // routes.IgnoreRoute("{*codegen}", new { codegen = @".*codegen\.aspx(/.*)?" });
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "AlbumSectionEditorView", // Route name
                "Albums/AlbumSectionEditorView/{album}/{id}", // URL with parameters
                new { controller = "Albums", action = "AlbumSectionEditorView", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "AlbumDesigner.Controllers", "Keepo.Designer.Controllers" }
            );

            routes.MapRoute(
                "EditorLogin", // Route name
                "Login/{id}", // URL with parameters
                new { controller = "Users", action = "EditorLogin", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "AlbumDesigner.Controllers", "Keepo.Designer.Controllers" }
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "AlbumDesigner.Controllers", "Keepo.Designer.Controllers" }
            );
        }

        protected void Application_Start()
        {
            // Bugfix - delete folders
            FixAppDomainRestart.FixAppDomainRestartWhenTouchingFiles();

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            // Initialize the Database
            Database.SetInitializer<AlbumContext>(new AlbumInitializer());

            // Use KeepoContext For Repositories
            ObjectContextManager.init(new AlbumContextCreator());
        }

        //when an error occur
        protected void Application_Error(object sender, EventArgs e)
        {
            var error = Server.GetLastError();
            var code = (error is HttpException) ? (error as HttpException).GetHttpCode() : 500;

            //if the error is not HttpNotFound (404)
            if (code != 404)
            {
                var traceFirstLine = error.StackTrace.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)[0];
                System.IO.File.AppendAllText(Server.MapPath("~/ErrorLog.txt"), DateTime.Now + "\r\n" + error.Message + "\r\n" + traceFirstLine + "\r\n\r\n");
            }
        }
    }
}